{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import FLib as f"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function add_pro_auto in module FLib:\n",
      "\n",
      "add_pro_auto(List)\n",
      "    [warning]:if you input such as x1y1, y1x1 will automatically added\n",
      "    [input_type]:str\n",
      "    [format]: name1,value1;name2,value2 ...\n",
      "    [value]: integer / integer (eg:3/4)\n",
      "    :return: (None)\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(f.add_pro_auto)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x1:0.4 successful added!\n",
      "x2:0.2 successful added!\n",
      "x3:0.2 successful added!\n",
      "x4:0.1 successful added!\n",
      "x5:0.05 successful added!\n",
      "x6:0.05 successful added!\n"
     ]
    }
   ],
   "source": [
    "f.add_pro_auto('x1,0.4;x2,0.2;x3,0.2;x4,0.1;x5,0.05;x6,0.05')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x7:0.19 successful added!\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "2.395928676331139"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f.add_pro_auto('x7,0.19')\n",
    "f.I_sef('x7')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ans = 1.32193 bit\n",
      "ans = 2.32193 bit\n",
      "ans = 2.32193 bit\n",
      "ans = 3.32193 bit\n",
      "ans = 4.32193 bit\n",
      "ans = 4.32193 bit\n"
     ]
    }
   ],
   "source": [
    "for i in range(1,7):\n",
    "    f.print_b(f.I_sef(f.cn_('x',i)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def Trans_real2binary(num, l):\n",
    "    s = ''\n",
    "    remain = num\n",
    "    for i in range(l):\n",
    "        remain = remain*2\n",
    "        if remain >= 1:\n",
    "            s = s + '1'\n",
    "            remain = remain - 1\n",
    "        else:\n",
    "            s = s + '0'\n",
    "    return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "00 2 0\n",
      "011 3 2/5\n",
      "100 3 3/5\n",
      "1100 4 4/5\n",
      "11100 5 9/10\n",
      "11110 5 19/20\n"
     ]
    }
   ],
   "source": [
    "w = [None, 2, 3, 3, 4, 5, 5]\n",
    "t = 0\n",
    "ave = 0.0\n",
    "for i in range(1, 7):\n",
    "    ans = Trans_real2binary(t,w[i])\n",
    "    print(ans+' '+str(w[i])+' '+str(t))\n",
    "    t = t + f.probability[f.cn_('x',i)]\n",
    "    ave = ave + f.probability[f.cn_('x',i)]*w[i]\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.9"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ave"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "f.clear_allMyPro()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x1:0.8 successful added!\n",
      "x2:0.2 successful added!\n"
     ]
    }
   ],
   "source": [
    "f.add_pro_auto('x1,0.8;x2,0.2')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.3219280948873623"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f.I_sef('x1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.7219280948873623"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f.H_sel('x',2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "y1|x1:5/6 successful added!\n",
      "y2|x1:1/6 successful added!\n",
      "y1|x2:3/4 successful added!\n",
      "y2|x2:1/4 successful added!\n"
     ]
    }
   ],
   "source": [
    "f.add_pro_auto('y1|x1,5/6;y2|x1,1/6;y1|x2,3/4;y2|x2,1/4')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "aim p(x1y1) not exist!\n",
      "x1y1:2/3 successful added!\n",
      "Automatically add x1y1:2/3 to database\n",
      "aim p(x1y2) not exist!\n",
      "x1y2:2/15 successful added!\n",
      "Automatically add x1y2:2/15 to database\n",
      "aim p(x2y1) not exist!\n",
      "x2y1:3/20 successful added!\n",
      "Automatically add x2y1:3/20 to database\n",
      "aim p(x2y2) not exist!\n",
      "x2y2:1/20 successful added!\n",
      "Automatically add x2y2:1/20 to database\n"
     ]
    }
   ],
   "source": [
    "for i in range(1,3):\n",
    "    for j in range(1,3):\n",
    "        f.p(f.cn_('x',i)+f.cn_('y',j))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "y1:49/60 successful added!\n",
      "y2:11/60 successful added!\n"
     ]
    }
   ],
   "source": [
    "f.add_pro_auto('y1,49/60;y2,11/60')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "# f.H_equivocation(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "# f.H_Noise(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "aim p(x1|y1) not exist!\n",
      "x1|y1:40/49 successful added!\n",
      "Automatically add x1|y1:40/49 to database\n",
      "aim p(x1|y2) not exist!\n",
      "x1|y2:8/11 successful added!\n",
      "Automatically add x1|y2:8/11 to database\n",
      "aim p(x2|y1) not exist!\n",
      "x2|y1:9/49 successful added!\n",
      "Automatically add x2|y1:9/49 to database\n",
      "aim p(x2|y2) not exist!\n",
      "x2|y2:3/11 successful added!\n",
      "Automatically add x2|y2:3/11 to database\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0.0050415306204173276"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f.H_mutual('x','y',2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "f.clear_allMyPro()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "z1:3/4 successful added!\n",
      "z2:1/4 successful added!\n",
      "x1:1/2 successful added!\n",
      "x2:1/2 successful added!\n",
      "y1:1/2 successful added!\n",
      "y2:1/2 successful added!\n",
      "x1y1:1/4 successful added!\n",
      "x1y2:1/4 successful added!\n",
      "x2y1:1/4 successful added!\n",
      "x2y2:1/4 successful added!\n"
     ]
    }
   ],
   "source": [
    "f.add_pro_auto('z1,3/4;z2,1/4;x1,1/2;x2,1/2;y1,1/2;y2,1/2')\n",
    "f.add_pro_auto('x1y1,1/4;x1y2,1/4;x2y1,1/4;x2y2,1/4')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.0"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f.H_combination('x','y',2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.8112781244591328"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f.H_sel('z',2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function H_Noise in module FLib:\n",
      "\n",
      "H_Noise(fir, sec, n)\n",
      "    [trans]: 噪声熵\n",
      "    [formula]: H(Y|X) = p(xi yj)*I(yj|xi) + ...\n",
      "    :param fir: variable 1 such as 'x' (str)\n",
      "    :param sec: variable 2 such as 'y' (str)\n",
      "    :param n: number of the max subscript (integer)\n",
      "    :return: (float)\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(f.H_Noise)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "aim p(x1|y1) not exist!\n",
      "x1|y1:1/2 successful added!\n",
      "Automatically add x1|y1:1/2 to database\n",
      "aim p(x1|y2) not exist!\n",
      "x1|y2:1/2 successful added!\n",
      "Automatically add x1|y2:1/2 to database\n",
      "aim p(x2|y1) not exist!\n",
      "x2|y1:1/2 successful added!\n",
      "Automatically add x2|y1:1/2 to database\n",
      "aim p(x2|y2) not exist!\n",
      "x2|y2:1/2 successful added!\n",
      "Automatically add x2|y2:1/2 to database\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f.H_equivocation('x','y',2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "z1x1:1/2 successful added!\n",
      "z2x1:0 successful added!\n",
      "z1x2:1/4 successful added!\n",
      "z2x2:1/4 successful added!\n"
     ]
    }
   ],
   "source": [
    "f.add_pro_auto('z1x1,1/2;z2x1,0;z1x2,1/4;z2x2,1/4')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/cxl/Documents/code/PycharmProjects/TheoryOfInformation/FLib.py:220: RuntimeWarning: divide by zero encountered in log2\n",
      "  return -np.log2(float(p(xCy)))\n",
      "/Users/cxl/opt/anaconda3/lib/python3.7/fractions.py:378: RuntimeWarning: invalid value encountered in double_scalars\n",
      "  return fallback_operator(float(a), b)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "nan"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f.H_equivocation('z','x',2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Fraction(1, 2)"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f._P_condition('z2','x2')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "z1|x1:1 successful added!\n",
      "z1|x2:1/2 successful added!\n",
      "z2|x1:0 successful added!\n",
      "z2|x2:1/2 successful added!\n"
     ]
    }
   ],
   "source": [
    "f.add_pro_auto('z1|x1,1;z1|x2,1/2;z2|x1,0;z2|x2,1/2')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import FLib as fb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.7021276595744681"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(4.7-1.4)/4.7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x1:0.7 successful added!\n",
      "x2:0.3 successful added!\n",
      "y1:0.64 successful added!\n",
      "y2:0.36 successful added!\n",
      "y1|x1:0.8 successful added!\n",
      "y2|x1:0.2 successful added!\n"
     ]
    }
   ],
   "source": [
    "fb.add_pro_auto('x1,0.7;x2,0.3;y1,0.64;y2,0.36;y1|x1,0.8;y2|x1,0.2')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x2y1:0.08 successful added!\n",
      "x2y2:0.22 successful added!\n"
     ]
    }
   ],
   "source": [
    "fb.add_pro_auto('x2y1,0.08;x2y2,0.22')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "aim p(x1|y1) not exist!\n",
      "x1|y1:7/8 successful added!\n",
      "Automatically add x1|y1:7/8 to database\n",
      "aim p(x1|y2) not exist!\n",
      "x1|y2:7/18 successful added!\n",
      "Automatically add x1|y2:7/18 to database\n",
      "aim p(x2|y1) not exist!\n",
      "x2|y1:1/8 successful added!\n",
      "Automatically add x2|y1:1/8 to database\n",
      "aim p(x2|y2) not exist!\n",
      "x2|y2:11/18 successful added!\n",
      "Automatically add x2|y2:11/18 to database\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0.18634130025198858"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fb.H_mutual('x','y',2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.07999999999999996"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.21999999999999997"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
